package com.ahau.springcloud.Controller;

import com.ahau.springcloud.entities.CommonResult;
import com.ahau.springcloud.entity.PayMent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author xiao hu die
 * @Date 2021/11/29 22:10
 * @Version 1.0
 */

@RestController
@Slf4j
public class OrderMainController {
    public static final String PAYMENT_URL ="http://CLOUD-PAYMENT-SERVICE";
    /**注入调用远程服务的对象*/
   @Resource
    private RestTemplate restTemplate;
   @GetMapping("/consumer/payment/insert")
    private CommonResult<PayMent> create(PayMent payMent){
      log.info("进入服务端的create方法");
       /**实现远程调用的重要中间类restTemplate*/
      return    restTemplate.postForObject(PAYMENT_URL +"/payMent/insert",payMent, CommonResult.class);
   }
   @GetMapping("/consumer/queryById/{id}")
   private CommonResult<PayMent> queryById(@PathVariable("id") Long id){
       CommonResult commonResult = restTemplate.getForObject(PAYMENT_URL + "/payMent/selectOne?"+"id="+id, CommonResult.class);
       return commonResult;
   }
   /**练习getForEntity*/
   @GetMapping("/consumer/getForEntity/{id}")
   public CommonResult<PayMent> queryById2(@PathVariable("id") Long id){
       ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payMent/selectOne?" + "id=" + id, CommonResult.class);
       if (forEntity.getStatusCode().is2xxSuccessful()){
           return forEntity.getBody();
       }else {
            CommonResult commonResult =new CommonResult(444, "操作失败", null);
            return commonResult;
       }
   }
}
